import { InputQuestion, QuestionBase, SelectQuestion, TextQuestion } from '@/components/public-template';
import { TextTable } from '@/components/public-template';
import { TableBase } from '@/components/public-template';
import { Api } from '@/config/api/archives';
import { regExp } from '@/utils/regExp';

class HomeService {
  getQuestions(record, showType) {
    const questions: QuestionBase<any>[] = [
      new TextQuestion({
        key: 'id',
        hide: true,
        value: null
      }),
      showType == 'EDIT'
        ? new TextQuestion({
            key: 'code',
            label: '档案盒编码',
            value: null
          })
        : new InputQuestion({
            key: 'code',
            label: '档案盒编码',
            required: true,
            prompt: '请输入不超过20个字符的有效字母或数字组合，不能为空',
            disabled: showType != 'ADD',
            value: null,
            reg: regExp.alphanumer
          }),
      new InputQuestion({
        key: 'boxName',
        label: '档案盒名称',
        prompt: '至多允许输入128个字符',
        value: null,
        reg: regExp.len128
      }),
      showType == 'EDIT'
        ? new TextQuestion({
            key: 'folderName',
            label: '存放地点',
            value: null
          })
        : new SelectQuestion({
            key: 'folderId',
            label: '存放地点',
            required: true,
            prompt: '请选择',
            disabled: [2, 0].includes(record.status),
            value: null,
            type: 'tree',
            state: {
              fullPath: true,
              url: Api.folderfindCanUseTree,
              protType: 'post',
              nameKey: 'name',
              nameValue: 'id',
              nameChildren: 'children',
              expandAll: true
            }
          }),
      new InputQuestion({
        key: 'spineThickness',
        label: '脊背厚度(cm)',
        type: 'number',
        prompt: '至多允许输入3位数，保留一位小数',
        value: null,
        reg: /^\d{1,3}(\.\d{1})?$/
      }),
      showType == 'EDIT'
        ? new TextQuestion({
            key: 'typeName',
            label: '档案类型',
            value: null
          })
        : new SelectQuestion({
            key: 'typeId',
            label: '档案类型',
            required: true,
            prompt: '请选择',
            disabled: [2, 0].includes(record.status),
            value: null,
            type: 'tree',
            state: {
              url: Api.typefindCanUseTree,
              protType: 'post',
              nameKey: 'name',
              nameValue: 'id',
              nameChildren: 'children'
            }
          }),
      showType == 'EDIT'
        ? new TextQuestion({
            key: 'retentionPeriodDate',
            label: '保管期限',
            type: 'dictionary',
            state: {
              filterName: 'DMS_ARCHIVES_FILE_LIST_RETENTION_PERIOD_DATE'
            }
          })
        : new SelectQuestion({
            key: 'retentionPeriodDate',
            label: '保管期限',
            required: true,
            prompt: '请选择',
            value: null,
            type: 'dictionary',
            state: {
              filterName: 'DMS_ARCHIVES_FILE_LIST_RETENTION_PERIOD_DATE'
            }
          }),
      showType == 'EDIT'
        ? new TextQuestion({
            key: 'secretLevel',
            label: '保密等级',
            type: 'dictionary',
            state: {
              filterName: 'SECRET_LEVEL'
            }
          })
        : new SelectQuestion({
            key: 'secretLevel',
            label: '保密等级',
            required: true,
            prompt: '请选择',
            value: null,
            type: 'dictionary',
            state: {
              filterName: 'SECRET_LEVEL'
            }
          }),
      new InputQuestion({
        key: 'startPartNo',
        label: '起始件号',
        value: null,
        disabled: [0].includes(record.status),
        hidden: showType == 'ADD'
      }),
      new InputQuestion({
        key: 'endPartNo',
        label: '终止件号',
        value: null,
        disabled: [0].includes(record.status),
        hidden: showType == 'ADD'
      }),
      new InputQuestion({
        key: 'name',
        label: '描述',
        value: null,
        type: 'textarea',
        state: {
          grid: 24
        },
        reg: regExp.len100,
        prompt: '描述至多允许输入100字符'
      })
    ];
    return questions;
  }
  getViewQuestions() {
    const questions: QuestionBase<any>[] = [
      new TextQuestion({
        key: 'code',
        label: '档案盒编码',
        value: null,
        disabled: true
      }),
      new TextQuestion({
        key: 'boxName',
        label: '档案盒名称',
        value: null,
        disabled: true
      }),
      new TextQuestion({
        key: 'folderName',
        label: '存放地点',
        value: null,
        disabled: true
      }),
      new TextQuestion({
        key: 'spineThickness',
        label: '脊背厚度(cm)',
        value: null,
        disabled: true
      }),
      new TextQuestion({
        key: 'typeName',
        label: '档案类型',
        value: null,
        disabled: true
      }),
      new TextQuestion({
        key: 'retentionPeriodDate',
        label: '保管期限',
        type: 'dictionary',
        state: {
          filterName: 'DMS_ARCHIVES_FILE_LIST_RETENTION_PERIOD_DATE'
        }
      }),
      new TextQuestion({
        key: 'secretLevel',
        label: '保密等级',
        type: 'dictionary',
        state: {
          filterName: 'SECRET_LEVEL'
        }
      }),
      new TextQuestion({
        key: 'startPartNo',
        label: '起始件号',
        value: null,
        disabled: true
      }),
      new TextQuestion({
        key: 'endPartNo',
        label: '终止件号',
        value: null,
        disabled: true
      }),
      new TextQuestion({
        key: 'name',
        label: '描述',
        value: null,
        disabled: true
      })
    ];
    return questions;
  }
  getTables() {
    const tables: TableBase<any>[] = [
      new TextTable({
        key: 'num',
        value: '序号',
        type: 'index',
        columns: {
          width: 70
        }
      }),
      new TextTable({
        key: 'lastStatus',
        value: '变更前状态',
        type: 'dictionary',
        state: {
          filterName: 'BOX_STATUS'
        }
      }),
      new TextTable({
        key: 'changeStatus',
        value: '变更后状态',
        type: 'dictionary',
        state: {
          filterName: 'BOX_STATUS'
        }
      }),
      new TextTable({
        key: 'reason',
        value: '状态变更原因'
      }),
      new TextTable({
        key: 'creatorName',
        value: '操作人'
      }),
      new TextTable({
        key: 'modifiedTime',
        value: '操作时间'
      })
    ];
    return tables;
  }
}
// 根据档案盒文件数，获取件号下拉项
function getPartOpt(num) {
  if (!num) return [];
  return Array.from(Array(num)).map((_, index) => {
    const label = String(index + 1).padStart(3, '0');
    return { label, value: label };
  });
}

export default new HomeService();
